﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class NguoiDungDAL:StandardConnectDB,INguoiDung
    {
        public List<DTO.NguoiDungDTO> GetlistNguoiDung()
        {
            List<NguoiDungDTO> listNguoiDung = new List<NguoiDungDTO>();
            DataTable dt = this.DTExecuteSelect("Select * from NguoiDung");
            foreach (DataRow dr in dt.Rows)
            {
                listNguoiDung.Add(ConvertObjectDataRowToNguoiDungDTO(dr));
            }
            return listNguoiDung;
            
        }

        public DTO.NguoiDungDTO GetNguoiDungByID(string MaND)
        {
            NguoiDungDTO nguoidung = new NguoiDungDTO();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@Username", MaND);
            DataTable dt = DTExecuteSelect("SP_NguoiDung_GetByID", paramlist);
           

            if (dt.Rows.Count > 0)
            {
                nguoidung = ConvertObjectDataRowToNguoiDungDTO(dt.Rows[0]);
                return nguoidung;
            }

            return null;
            
        }

        public List<DTO.NguoiDungDTO> GetAllNguoiDungByMaNhom(string MaNhom)
        {
            List<NguoiDungDTO> listNguoiDung = new List<NguoiDungDTO>();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaNhom", MaNhom);
            DataTable dt = this.DTExecuteSelect("SP_NguoiDung_GetByMaNhom",paramlist);
            foreach (DataRow dr in dt.Rows)
            {
                listNguoiDung.Add(ConvertObjectDataRowToNguoiDungDTO(dr));
            }
            return listNguoiDung;
            
        }

        public bool InsertNguoiDung(DTO.NguoiDungDTO nguoidung)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
         
            paramlist.Add("@Username", nguoidung.Username);
            paramlist.Add("@Password", nguoidung.Password);
            paramlist.Add("@MaNhom", nguoidung.MaNhom.MaNhom);
            paramlist.Add("@Email", nguoidung.Email);
            paramlist.Add("@HoTen", nguoidung.HoTen);
            paramlist.Add("@NgaySinh", nguoidung.NgaySinh);
            paramlist.Add("@SoDT", nguoidung.SoDT);
            paramlist.Add("@DiaChi", nguoidung.DiaChi);
            paramlist.Add("@GioiTinh", nguoidung.GioiTinh);
            return ExecuteUpdate("SP_NguoiDung_Insert", paramlist);
       
        }

        public bool DeleteNguoiDung(DTO.NguoiDungDTO nguoidung)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

        //    paramlist.Add("@MaND", nguoidung.MaND);

            return this.ExecuteUpdate("SP_NguoiDung_Delete", paramlist);
           
        }

        public bool UpdateNguoiDung(DTO.NguoiDungDTO nguoidung)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@Username", nguoidung.Username);
            paramlist.Add("@Password", nguoidung.Password);
            paramlist.Add("@MaNhom", nguoidung.MaNhom.MaNhom);
            paramlist.Add("@Email", nguoidung.Email);
            paramlist.Add("@HoTen", nguoidung.HoTen);
            paramlist.Add("@NgaySinh", nguoidung.NgaySinh);
            paramlist.Add("@SoDT", nguoidung.SoDT);
            paramlist.Add("@DiaChi", nguoidung.DiaChi);
            paramlist.Add("@GioiTinh", nguoidung.GioiTinh);
         
            return ExecuteUpdate("SP_NguoiDung_Update", paramlist);
            
        }

        public DTO.NguoiDungDTO ConvertObjectDataRowToNguoiDungDTO(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                NguoiDungDTO nguoidung = new NguoiDungDTO();
               
                INhomNguoiDung inhomnguoidung = new NhomNguoiDungDAL();

                if (dr["MaNhom"].ToString() != "")
                    nguoidung.MaNhom = inhomnguoidung.GetNhomNguoiDungByID(dr["MaNhom"].ToString());

                nguoidung.Username = dr["UserName"].ToString();
                nguoidung.Password = dr["Password"].ToString();
                nguoidung.HoTen = dr["HoTen"].ToString();
                return nguoidung;
            }
            else
                return null;
            
        }
    }
}
